<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class order_controller extends admin_controller
{
    // Get list
    public function __construct()
    {
        parent::__construct();
        if(!$this->is_admin_logged()) redirect(base_url(). $this->base_admin_url .'login_controller');
        $this->page_title = 'Bàn';
        $this->table_name = 'orders';
        $this->route_name = 'order_controller';
        $this->business_model = $this->order_model;
    }

    public function index()
    {
        $header_data = array();
        $header_data['show_menu'] = true;
        $view_data = $this->get_view_data();
        $view_data['list'] = $this->get_list();
        $view_data['actions'] = $this->get_list_actions();
        $this->load_view('list_template',$header_data,$view_data);
    }

    public function get_list_actions()
    {
        $actions = array();
        $actions[] = array(
            'text' => 'Xoá',
            'href' => base_url() . $this->base_admin_url . $this->route_name . '/delete',
            'view_change_method' => 'submit'
        );
        return $actions;
    }

    public function get_form_actions($id)
    {
        $actions = array();
        $actions[] = array(
            'text' => 'Quay lại',
            'href' => $this->hook_cancel_url($id),
            'view_change_method' => 'redirect'
        );
        return $actions;
    }

    public function get_list()
    {
        $list = array();
        $news = $this->business_model->get_elements();
        foreach($news as $item)
        {
            $actions = array();
            $actions[] = array(
                'text' => 'Xem',
                'href' => base_url(). $this->base_admin_url .$this->route_name.'/update/'.$item->id
            );
            $list[] = array(
                'can_delete' => $item->id,
                'id' => $item->id,
                'table_id' => $this->table_model->get_element_by_id($item->table_id)->name,
                'created_date' => ($item->created_date == '0') ? '' : date('d-m-Y H:i:s',$item->created_date),
                'modified_date' => date('d-m-Y H:i:s',$item->modified_date),
                'actions' => $actions
            );
        }
        return $list;
    }

    public function get_header_list()
    {
        $list = array();
        $list[] = 'ID';
        $list[] = 'Tên bàn';
        $list[] = 'Ngày tạo';
        $list[] = 'Ngày cập nhật';
        $list[] = 'Action';
        return $list;

    }

    public function hook_modify_post_data_before_insert($post_data)
    {
        $post_data['created_date'] = time();
        return $post_data;
    }

    public function get_post_data()
    {
        $post_data = array(
            'table_id' => $this->input->post('table_id'),
            'product_id' => $this->input->post('product_id'),
            'quantity' => $this->input->post('quantity'),

            'modified_date' => time(),
        );
        return $post_data;

    }

    public function get_fields($id)
    {

        if($id == -1)
            $model = new $this->business_model();
        else
            $model = $this->business_model->get_element_by_id($id);

        $order_detail = $this->order_detail_model->get_elements_by_order_id($id);

        $fields = array();

        $fields[] = array(
            'text' => 'Tên bàn',
            'name' => 'table_id',
            'value' => $model->table_name,
            'type' => 'label'
        );

        $fields[] = array(
            'text' => 'Ngày nhập',
            'name' => 'created_date',
            'value' => date('d-m-Y ----- h:m:i',$model->created_date),
            'type' => 'label'
        );

        $fields[] = array(
            'text' => 'Chi tiết',
            'name' => '',
            'value' => $order_detail,
            'type' => 'child-list'
        );

        $fields[] = array(
            'text' => 'Tổng',
            'name' => 'total',
            'value' => $model->total,
            'type' => 'label'
        );


        return $fields;
    }

}
